﻿@{
    ViewData["Title"] = "盘点单管理";
}
<div class="box2" panelTitle="检索条件">
    <form id="queryForm" method="post">
        <table formMode="transparent">
            <tr>
                <td>&nbsp;&nbsp;订单号：</td>
                <td><input type="text" name="orderNo" id="orderNo" /></td>
                <td>&nbsp;&nbsp;单据类型：</td>
                <td>
                    <select prompt="全部" url="/wms/CycleCountManage/GetCheckStockType" name="orderType" id="orderType">
                    </select>
                </td>
                <td>&nbsp;&nbsp;单据状态：</td>
                <td>
                    <select prompt="全部" url="/wms/CycleCountManage/GetCheckStockStatus" name="orderStatus" id="orderStatus">
                    </select>
                </td>
                <td>
                    &nbsp;&nbsp;创建时间：
                </td>
                <td>
                    <input type="text" class="date" name="startTime" style="width:auto" id="startTime" dateFmt="yyyy-MM-dd HH:mm:ss" />
                </td>
                <td>
                    &nbsp;&nbsp;至：
                </td>
                <td>
                    <input type="text" class="date" name="endTime" style="width:auto" id="endTime" dateFmt="yyyy-MM-dd HH:mm:ss" />
                </td>
                <td>&nbsp;&nbsp;是否启用：</td>
                <td>
                    <select name="enabled" id="enabled" selectedValue="Y" data='{"list":[{"value":"","key":"全部"},{"value":"Y","key":"是"},{"value":"N","key":"否"}]}'></select>
                </td>
                <td><button type="button" onclick="searchHandler()"><span class="icon_find">查询</span></button></td>
                <td><button type="button" onclick="resetSearch()"><span class="icon_reload">重置</span></button></td>
            </tr>
        </table>
    </form>
</div>
<div class="padding_right">
    <div class="basicTab" id="myTab">
        <div name="盘点单">
            <div id="maingrid"></div>
        </div>
    </div>
</div>

<script type="text/javascript">
    var grid;
    function initComplete() {
        grid = $("#maingrid").quiGrid({
            columns: [
                { display: '盘点单号', name: 'orderNo', align: 'center', width: "10%" },
                { display: '零件号', name: 'partNo', align: 'center', width: "20%" },
                { display: '库存地', name: 'erpWarehouse', align: 'center', width: "10%" },
                { display: '单据类型', name: 'orderTypeDesc', align: 'center', width: "10%" },
                { display: '单据状态', name: 'orderStatusDesc', align: 'center', width: "10%" },
                { display: '创建时间', name: 'createTime', align: 'center', width: "14%" },
                {
                    display: '是否明盘', name: 'isObvious', align: 'center', width: "10%",
                    render: function(rowdata, rowindex, value, column) {
                        if (value == "1") {
                            return "是";
                        } else {
                            return "否";
                        }
                    }
                },
                { display: '工厂代码', name: 'factoryCode', align: 'center', width: "8%" },
                {
                    display: '启用/禁用', name: 'enabled', align: 'center', width: "8%",
                    render: function(rowdata, rowindex, value, column) {
                        if (value == "Y") {
                            return "启用";
                        } else {
                            return "禁用";
                        }
                    }
                }
            ],
            url: '/wms/CycleCountManage/getCheckStockByPage', sortName: 'orderNo', rownumbers: true, checkbox: true,
            height: '100%', width: "100%", percentWidthMode: true, pageSize: 100,
            //顶部图标按钮栏
            toolbar: {}
        });

        //载入工具栏图标
        loadToolbar("", grid);
        addDoubleClick("", grid);


        $("#myTab").bind("actived", function(e, idx) {
            if (idx == 0) {
                grid.resetHeight();
                grid.resetWidth();
            }
        })
    }

    //查询
    function searchHandler() {
        //得到查询参数
        var query = $("#queryForm").formToArray();
        //将查询参数传给grid表格
        grid.setOptions({ params: query });
        //页号重置为1
        grid.setNewPage(1);
        //重新加载数据
        grid.loadData();

    }

    //重置查询
    function resetSearch() {
        $("#queryForm")[0].reset();
        searchHandler();
    }

    //添加
    function onAdd() {
        top.Dialog.open({ URL: "/wms/CycleCountManage/EditCycleCountManage", Title: "新增", Width: 900, Height: 1300 });
    }

    //开始盘点
    function onBeginCycle() {
        var rows = grid.getSelectedRows();
        var rowsLength = rows.length;
        var rowid = "";
        if (rowsLength == 0) {
            top.Dialog.alert("请选中要盘点的记录!");
            return;
        }

        for (var i = 0; i < rows.length; i++) {
            if (rows[i].orderStatus != "10") {
                top.Dialog.alert(rows[i].orderNo + ":该盘点单不是已创建状态");
                return;
            }
        }

        top.Dialog.confirm("是否确认开始盘点？", function() {
            $.post("/wms/CycleCountManage/onBeginCycle",
                //获取所有选中行
                getSelectOrderNo(grid),
                function(result) {
                    if (result.status != -1) {
                        top.Dialog.alert(result.message);
                        grid.loadData();
                    } else {
                        top.Dialog.alert("开始盘点指令失败！");
                    }
                },
                "json");
        });
    }


    //删除后的提示
    function handleResult(result) {
        if (result != -1) {
            top.Dialog.alert("删除成功！删除" + result + "条数据", null, null, null, 1);
            grid.loadData();
        } else {
            top.Dialog.alert("删除失败！");
        }
    }

    //完成盘点提示
    function CompletehandleResult(result) {
        if (result.status != -1) {
            top.Dialog.alert(result.message);
            grid.loadData();
        } else {
            top.Dialog.alert("完成盘点指令失败！");
        }
    }
    //完成盘点
    function onCompleteCycle() {
        var rows = grid.getSelectedRows();
        var rowsLength = rows.length;

        if (rowsLength == 0) {
            top.Dialog.alert("请选择盘点指令!");
            return;
        }

        if (rows[0].orderStatus != "20" && rows[0].orderStatus != "30") {
            top.Dialog.alert("该盘点单状态不允许完成盘点");
            return;
        }
        if (rows[0].orderStatus == "60") {
            top.Dialog.alert("该盘点单已关闭");
            return;
        }
        if (rows[0].enabled != "Y") {
            top.Dialog.alert("该盘点单已禁用");
            return;
        }
        top.Dialog.confirm("是否确认完成盘点？", function() {
            $.post("/wms/CycleCountManage/onCompleteCycle",
                //获取所有选中行
                getSelectOrderNo(grid),
                function(result) {
                    CompletehandleResult(result);
                },
                "json");
        });
    }

    //冻结提示
    function FreezehandleResult(result) {
        if (result.status != -1) {
            top.Dialog.alert(result.message);
            grid.loadData();
        } else {
            top.Dialog.alert("盘点指令冻结失败！");
        }
    }
    //关闭盘点
    function onCloseCycle() {
        var rows = grid.getSelectedRows();
        var rowsLength = rows.length;

        if (rowsLength == 0) {
            top.Dialog.alert("请选择盘点指令!");
            return;
        }
        for (var i = 0; i < rows.length; i++) {
            if (rows[i].orderStatus != "10") {
                top.Dialog.alert(rows[i].orderNo + ":该盘点单不是已创建状态");
                return;
            }
        }

        top.Dialog.confirm("确认是否要关闭选中数据？", function() {
            $.post("/wms/CycleCountManage/onCloseCycle",
                //获取所有选中行
                getSelectOrderNo(grid),
                function(result) {
                    FreezehandleResult(result);
                },
                "json");
        });

    }

    //整单解冻调差
    function onAdjustment() {
        var rows = grid.getSelectedRows();
        var rowsLength = rows.length;

        if (rowsLength == 0) {
            top.Dialog.alert("请选择盘点指令!");
            return;
        } else if (rowsLength > 1) {
            top.Dialog.alert("只能选择一行");
            return;
        }

        if (rows[0].orderStatus != "40") {
            top.Dialog.alert(rows[i].orderNo + ":该盘点单状态不允许解冻调差");
            return;
        }
        if (rows[0].enabled != "Y") {
            top.Dialog.alert("该盘点单已关闭");
            return;
        }
        top.Dialog.confirm("是否确认整单解冻调差？", function() {
            $.post("/wms/CycleCountManage/onAdjustment",
                //获取所有选中行
                getSelectOrderNo(grid),
                function(result) {
                    CompletehandleResult(result);
                },
                "json");
        });
    }

    //整单解冻不调差
    function onNoAdjustment() {
        var rows = grid.getSelectedRows();
        var rowsLength = rows.length;

        if (rowsLength == 0) {
            top.Dialog.alert("请选择盘点指令!");
            return;
        }

        for (var i = 0; i < rows.length; i++) {
            if (rows[i].orderStatus != "40") {
                top.Dialog.alert(rows[i].orderNo + ":该盘点单状态不允许解冻不调差");
                return;
            }

            if (rows[i].enabled != "Y") {
                top.Dialog.alert("该盘点单已禁用");
                return;
            }
        }

        top.Dialog.confirm("是否确认整单解冻不调差？", function() {
            $.post("/wms/CycleCountManage/onNoAdjustment",
                //获取所有选中行
                getSelectOrderNo(grid),
                function(result) {
                    if (result.status != -1) {
                        top.Dialog.alert(result.message);
                        grid.loadData();
                    } else {
                        top.Dialog.alert("整单解冻不调差指令失败！");
                    }
                },
                "json");
        });
    }

    //复盘
    function onReplay() {
        var rows = grid.getSelectedRows();
        var rowsLength = rows.length;

        if (rowsLength == 0) {
            top.Dialog.alert("请选择盘点指令!");
            return;
        } else if (rowsLength > 1) {
            top.Dialog.alert("只能选择一行");
            return;
        }
         if (rows[0].orderStatus != "40") {
            top.Dialog.alert("该盘点单不是完成盘点状态，不允许复盘！");
            return;
        }
          top.Dialog.confirm("是否确认复盘？", function() {
            $.post("/wms/CycleCountManage/onReplay",
                //获取所有选中行
                getSelectOrderNo(grid),
                function(result) {
                    if (result.status != -1) {
                        top.Dialog.alert(result.message);
                        grid.loadData();
                    } else {
                        top.Dialog.alert("复盘指令失败！");
                    }
                },
                "json");
        });

    }

    //双击
    function onDblClickRow(rowdata, rowid, rowobj) {
        top.frmright.tabCloseHandler(1211);
        top.frmright.tabAddHandler(1211, "盘点单明细", "/wms/CycleCountManage/CheckStockDetailIndex?orderNo=" + rowdata.orderNo);
    }

    function getSelectOrderNo(grid) {
        var selectedRows = grid.getSelectedRows();
        var selectedRowsLength = selectedRows.length;
        var orderNo = "";

        for (var i = 0; i < selectedRowsLength; i++) {
            orderNo += selectedRows[i].orderNo + ",";
        }
        return { "orderNo": orderNo };
    }

    //获取所有选中行获取选中行的id 格式为 ids=1&ids=2
    function getSelectId(grid) {
        var selectedRows = grid.getSelectedRows();
        var selectedRowsLength = selectedRows.length;
        var ids = "";

        for (var i = 0; i < selectedRowsLength; i++) {
            ids += selectedRows[i].ruid + ",";
        }
        return { "ids": ids };
    }
    //禁用
    function ClosehandleResult(result) {
        if (result != -1) {
            top.Dialog.alert("单据关闭成功！", null, null, null, 1);
            grid.loadData();
        } else {
            top.Dialog.alert("单据关闭失败！");
        }
    }

    //关闭
    function onClose() {
        $.post("/wms/CycleCountManage/onClose",
            //获取所有选中行
            getSelectId(grid),
            function(result) {
                ClosehandleResult(result.status);
            },
            "json");
    }

    //启用
    function EnablehandleResult(result) {
        if (result != -1) {
            top.Dialog.alert("启用成功！", null, null, null, 1);
            grid.loadData();
        } else {
            top.Dialog.alert("启用失败！");
        }
    }

    //禁用
    function DisablehandleResult(result) {
        if (result != -1) {
            top.Dialog.alert("禁用成功！", null, null, null, 1);
            grid.loadData();
        } else {
            top.Dialog.alert("禁用失败！");
        }
    }
    //启用
    function onEnable(rowid, rowidx) {
        var rows = grid.getSelectedRows();
        var rowsLength = rows.length;

        if (rowsLength == 0) {
            top.Dialog.alert("请选中要启用的记录!");
            return;
        }
        $.post("/wms/CycleCountManage/onEnable",
            //获取所有选中行
            getSelectId(grid),
            function(result) {
                EnablehandleResult(result.status);
            },
            "json");
    }
    //禁用
    function onDisable(rowid, rowidx) {
        var rows = grid.getSelectedRows();
        var rowsLength = rows.length;

        if (rowsLength == 0) {
            top.Dialog.alert("请选中要禁用的记录!");
            return;
        }
        top.Dialog.confirm("确定要禁用吗？", function() {
            $.post("/wms/CycleCountManage/onDisable",
                //获取所有选中行
                getSelectId(grid),
                function(result) {
                    DisablehandleResult(result.status);
                },
                "json");
        });
    }

    //导出处理
    function exportData() {
        var rows = grid.getSelectedRows();
        var rowsLength = rows.length;

        if (rowsLength == 0) {
            top.Dialog.alert("请选择需要导出的盘点单!");
            return;
        }
        if (rowsLength > 1) {
            top.Dialog.alert("只能选择一行");
            return;
        }

        var url = "/wms/CycleCountManage/exportData?orderNo=" + rows[0].orderNo;
        window.location = url;
    }

    //刷新表格 表单提交的回调
    function afterFormSubmit() {
        grid.loadData();
    }

    //刷新表格数据并重置排序和页数
    function refresh(isUpdate) {
        if (!isUpdate) {
            //重置排序
            grid.options.sortName = 'orderNo';
            grid.options.sortOrder = "desc";
            //页号重置为1
            grid.setNewPage(1);
        }
        grid.loadData();
    }

</script>	